using SmartCommunity.Services.Community;
using Microsoft.AspNetCore.Mvc;

namespace SmartCommunity.Controllers.Community
{
    /// <summary>
    /// API接口：
    /// </summary>
    [ApiController]
    [Route("api/community/[controller]")]
    public class BuildingController : BaseController
    {
        #region 构造函数
        private readonly BuildingService buildingService;
        public BuildingController(BuildingService buildingService)
        {
            this.buildingService = buildingService;
        }
        #endregion

        #region 查询导出 
        [HttpGet("area-tree")]
        public async Task<IEnumerable<SelectOption>> GetAreaTree()
        {
            return await buildingService.GetAreaTree();
        }
        [HttpGet("building-tree")]
        public async Task<IEnumerable<BuildingDto>> GetBuildingTree()
        {
            return await buildingService.GetBuildingTree();
        }
        /// <summary>
        /// 根据小区ID获取主楼宇
        /// </summary>
        [HttpGet("main-building")]
        public async Task<IEnumerable<SelectOption>> GetBuildingByAreaId(Guid areaId)
        {
            var lst = await buildingService.GetBuildingByAreaId(areaId);
            return lst.Select(a => new SelectOption(a.BuildingId, a.BuildingName));
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        [HttpGet]
        public async Task<PageResult<BuildingDto>> GetList([FromQuery] BuildingQuery query, [FromQuery] Pager pager)
        {
            return await buildingService.FindPagedList(query, pager, CurrentUser);
        }

        /// <summary>
        /// 通过主键查询
        /// </summary>
        /// <param name="id">主键ID</param>
        [HttpGet("{id}")]
        public async Task<BuildingDto> GetOne(Guid id)
        {
            return await buildingService.FindBuildingById(id);
        }

        /// <summary>
        /// 导出excel
        /// </summary>
        [HttpGet("export")]
        public async Task<ActionResult> ExportExcel([FromQuery] BuildingQuery query)
        {
            var pageData = await buildingService.FindPagedList(query, Pager.Unpaged, CurrentUser);
            var exporter = new ExcelExporter();
            exporter.AddTitleRow(0, "小区名称", "楼宇名称", "楼宇类型", "建筑层数", "建筑类型", "建筑用途", "楼房朝向", "备注说明");
            int rowIndex = 1;
            foreach (var data in pageData.DataList)
            {
                exporter.AddDataRow(rowIndex++, 0, data.AreaName, data.BuildingName, data.BuildingLevelName, data.Floor, data.BuildingTypeName, data.BuildingUsageName, data.BuildingDirectionName, data.Remark);
            }
            var bytes = exporter.GetExcelData();
            return File(bytes, CONTENT_TYPE_EXCEL, $"小区楼宇导出{DateTime.Now:yyyyMMddHHmmss}.xlsx");
        }
        #endregion

        #region 增 删 改
        /// <summary>
        /// 新增
        /// </summary>
        [HttpPost]
        [Log("新增", OperateType.Create, false, true)]
        public async Task<ActionResult<BuildingDto>> Create([FromBody] BuildingDto dto)
        {
            var building = await buildingService.CreateBuilding(dto, CurrentUser);
            return CreatedAtAction("GetOne", new { id = building.BuildingId }, building);
        }

        /// <summary>
        /// 修改
        /// </summary>
        [HttpPut("{id}")]
        [Log("修改", OperateType.Update)]
        public async Task<ActionResult> Update(Guid id, [FromBody] BuildingDto dto)
        {
            if (id != dto.BuildingId)
            {
                return ErrorMessage("要更新的数据和ID不匹配");
            }
            await buildingService.UpdateBuilding(dto, CurrentUser);
            return NoContent();
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpDelete]
        [Log("删除", OperateType.Delete)]
        public async Task<ActionResult> Delete([FromQuery] Guid[] ids)
        {
            if (ids.Length == 0)
            {
                return ErrorMessage("参数错误：没有接收到要删除的主键值");
            }
            await buildingService.DeleteByIds(ids, CurrentUser);
            return NoContent();
        }
        #endregion
    }
}
